package com.ruoyi.project.bid.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.project.bid.domain.Template;
import com.ruoyi.project.bid.domain.UserDownload;
import com.ruoyi.project.bid.domain.UserDownloadDetail;
import com.ruoyi.project.bid.domain.vo.TemplateDetailVO;
import com.ruoyi.project.bid.mapper.TemplateMapper;
import com.ruoyi.project.bid.service.ITemplateService;
import com.ruoyi.project.bid.service.IUserDownloadDetailService;
import com.ruoyi.project.bid.service.IUserDownloadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 部门管理 服务实现
 * 
 * @author lidong
 */
@Service
public class TemplateServiceImpl implements ITemplateService
{
    @Autowired
    private TemplateMapper templateMapper;
    @Autowired
    private IUserDownloadService downloadService;
    @Autowired
    private IUserDownloadDetailService downloadDetailService;

    @Override
    @DataScope(deptAlias = "d")
    public List<Template> selectTemplateList(Template template)
    {
        return templateMapper.selectTemplateList(template);
    }

    @Override
    public List<Template> selectTemplateListByIds(List<Long> ids) {
        return templateMapper.selectTemplateListByIds(ids);
    }

    @Override
    public List<Template> selectTemplateListByDownload(TemplateDetailVO templateDetailVO) {
        return templateMapper.selectTemplateListByDownload(templateDetailVO);
    }

    @Override
    public int insertDownloadRecord(List<Template> templates) {
        UserDownload userDownload = new UserDownload();
        userDownload.setUserId(SecurityUtils.getCurrUserId());
        userDownload.setCreateBy(SecurityUtils.getUsername());
        downloadService.insertUserDownload(userDownload);

        templates.forEach(template -> {
            UserDownloadDetail detail = new UserDownloadDetail();
            detail.setDownloadId(userDownload.getId());
            detail.setTemplateId(template.getTemplateId());
            detail.setCreateBy(SecurityUtils.getUsername());
            downloadDetailService.insertUserDownloadDetail(detail);
        });

        return 1;
    }

    @Override
    public int insertTemplate(Template template)
    {
        template.setDelFlag("0");
        return templateMapper.insertTemplate(template);
    }

    @Override
    public int updateTemplate(Template template)
    {
        return templateMapper.updateTemplate(template);
    }

    @Override
    public int deleteTemplateById(Long templateId)
    {
        return templateMapper.deleteTemplateById(templateId);
    }

}
